#!/usr/bin/perl
use strict;

#my ($ITER, $RANGE) = (20000000, 500000, 25000);
my ($ITER, $RANGE) = (20000, 500);
my @species = qw(human dog monkey puppy);


sub compare_arrays(\@\@$)
{
	my ($arr1,  $arr2,  $name) = @_;
	if (scalar @$arr1 != scalar @$arr2)
	{
		die "FAILED $name ". scalar @$arr1. "!=". scalar @$arr2. "\n";;
	}
	for my $i (0.. (@$arr1 - 1) )
	{
		if ($arr1->[$i] ne $arr2->[$i])
		{
			print STDERR "$arr1->[$i] ne $arr2->[$i]\n";
			die "FAILED $name" ;
		}
	}
}


print STDERR "\tGenerating test files\n";
my %identifiers;
my %prot_to_gene_ids;
my %gene_ids_to_species;

{
	open RESULTS, ">results.test";
	my $cnt_results = 0;
	print STDERR "\t[";
	for (0..($ITER - 1) )
	{
		
		print STDERR "." if ($_ % ($ITER / 50) == 0);
		++$cnt_results;
		my $id1 =  int(rand($RANGE));
		my $id2 =  int(rand($RANGE));
	
		my $line = "id_$id1\tid_$id2\tresult $cnt_results\n";
		print RESULTS $line;
		$identifiers{"id_$id1"}++;
		$identifiers{"id_$id2"}++;
		my $gi1 = int($id1 / 10);
		my $gi2 = int($id2 / 10);
		$prot_to_gene_ids{"id_$id1"} = "id_$gi1";
		$prot_to_gene_ids{"id_$id2"} = "id_$gi2";
		$gene_ids_to_species{"id_$gi1"} = $gi1 % 4;
		$gene_ids_to_species{"id_$gi2"} = $gi2 % 4;
	}
	print STDERR "]\n";
}
my @identifiers = sort keys %identifiers;




#
#	print map
#
{
	open (MAP,  ">species.map") or die;
	for my $write_species_id(0.. (@species- 1))
	{
		for my $prot_id (keys %prot_to_gene_ids)
		{
			my $gene_id = $prot_to_gene_ids{$prot_id};
			my $species_id = $gene_ids_to_species{$gene_id};
			next unless $write_species_id == $species_id;
			print MAP "$prot_id\t$gene_id\t".$species[$species_id]."\n";
		}
	}
}


#
#	list of genes
#
my @genes;
my @prots;
{
	for my $prot_id (keys %prot_to_gene_ids)
	{
		my $gene_id = $prot_to_gene_ids{$prot_id};
		my $species_id = $gene_ids_to_species{$gene_id};
		push(@{$prots[$species_id]},  $prot_id);
		$genes[$species_id]{$gene_id}++;
	}
	for my $species_id(0.. (@genes- 1))
	{
		@{$prots[$species_id]} = sort @{$prots[$species_id]};
		$genes[$species_id] = [sort keys %{$genes[$species_id]}];
	}

	for my $species_id(0.. (@species- 1))
	{
		print "\t\t", scalar @{$prots[$species_id]}, "\tused identifiers from species\t". $species[$species_id] ."\n";
	}
	for my $species_id(0.. (@species- 1))
	{
		print "\t\t", scalar @{$genes[$species_id]}, "\tused genes from species\t". $species[$species_id] ."\n";
	}
}





print STDERR "\tRunning seq_pairs_list_identifiers\n";
system("seq_pairs_list_identifiers -m species.map < results.test -P prot.ids  -G gene.ids");
system("seq_pairs_list_identifiers -m species.map -v < results.test -P prot.[SPECIES].ids  -G gene.[SPECIES].ids");
print STDERR ("seq_pairs_list_identifiers -m species.map < results.test -P prot.ids  -G gene.ids\n");                        
print STDERR ("seq_pairs_list_identifiers -m species.map -v < results.test -P prot.[SPECIES].ids  -G gene.[SPECIES].ids\n"); 
#
#	read in derived and original identifiers
#
my @identifiers2;
{
	my %identifiers2;
	open IDS, "prot.ids" or die;
	for (<IDS>)
	{
		chomp;
		$identifiers2{$_}++;
	}
	@identifiers2 = sort keys %identifiers2;
}

for my $species_id(0.. (@species- 1))
{
	{
		open IDS, "gene.$species[$species_id].ids" or die;
		my @ARR = <IDS>;
		chomp @ARR;
		@ARR = sort @ARR;
		compare_arrays(@{$genes[$species_id]}, @ARR, "genes from $species[$species_id]\n");
	}
	{
		open IDS, "prot.$species[$species_id].ids" or die;
		my @ARR = <IDS>;
		chomp @ARR;
		@ARR = sort @ARR;
		compare_arrays(@{$prots[$species_id]}, @ARR, "prots from $species[$species_id]\n");
	}
}

compare_arrays(@identifiers, @identifiers2, "protein ids");

print STDERR "\tCleaning up...\n";
#unlink (glob("*.ids"));
#unlink (glob("*.test"));
#unlink (glob("*.map"));
print STDERR "\n\tSUCCESS!!!\n\n";

